package cn.dankal.lequ.ui.fragment.comprehensive_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.dankal.lequ.R;
import cn.dankal.lequ.commom.adapter.BaseAdapter.ViewHolder;
import cn.dankal.lequ.commom.adapter.BaseAdapter.recyclerview.MultiItemCommonAdapter;
import cn.dankal.lequ.commom.adapter.BaseAdapter.recyclerview.MultiItemTypeSupport;
import cn.dankal.lequ.domain.model.model;

/**
 * Created by 苏彬 on 2016/4/16.
 */
public class ClassifyFragment extends Fragment {
    @Bind(R.id.recyclerview)
    RecyclerView id_home_tab_recyclerview;
    LinearLayoutManager linearLayoutManager;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recyclerview, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecyclerView();
    }

    private void initRecyclerView() {
        linearLayoutManager = new LinearLayoutManager(getActivity());
        id_home_tab_recyclerview.setLayoutManager(linearLayoutManager);
        id_home_tab_recyclerview.setHasFixedSize(true);
        final List<model> mDatas = new ArrayList<>();
        model mmodle = null;
        mmodle = new model();
        mmodle.setGameId(321);
        mmodle.setName("武神赵子龙");
        mmodle.setImageUrl("https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/tam-ogel/735037da15769f7387dd9187aa17166a_121_121.jpg");
        mmodle.setGameType("呵呵");
        mDatas.add(mmodle);
        for (int i = 0; i < 4; i++) {
            mmodle = new model();
            mmodle.setGameId(123);
            mmodle.setName("武神赵子龙");
            mmodle.setImageUrl("https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/tam-ogel/735037da15769f7387dd9187aa17166a_121_121.jpg");
            mmodle.setGameType("角色扮演");
            mDatas.add(mmodle);
        }
        MultiItemTypeSupport<model> modelMultiItemTypeSupport = new MultiItemTypeSupport<model>() {
            @Override
            public int getLayoutId(int itemType) {
                if (itemType == 0) {
                    return R.layout.item_classify_header;
                } else {
                    return R.layout.item_classify_body;
                }
            }

            @Override
            public int getItemViewType(int position, model model) {
                if (model.getGameType().equals("角色扮演")) {
                    return 1;
                } else
                    return 0;
            }
        };
        id_home_tab_recyclerview.setAdapter(new MultiItemCommonAdapter<model>(getActivity(), mDatas, modelMultiItemTypeSupport) {
            @Override
            public void convert(ViewHolder holder, model model) {
                if (holder.getLayoutId() == R.layout.item_classify_header) {
                    holder.setOnClickListener(R.id.all_networkgame, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(getActivity(), "网络游戏", Toast.LENGTH_SHORT).show();
                        }
                    });
                } else {
                    holder.setOnClickListener(R.id.all_cosplay, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(getActivity(), "角色扮演", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }
}
